﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TiledShell.Models;

namespace TiledShell.Views
{
    /// <summary>
    /// Interaction logic for TileView.xaml
    /// </summary>
    public partial class TileView : UserControl
    {
        private Tile _tile;

        public TileView(Tile tile)
        {
            InitializeComponent();

            _tile = tile;
            var iconSource = _tile.GetIcon();
            
            var icon = new Image();
            icon.Source = iconSource;

            
            Color average = GetAverageColor(iconSource);  
            BrushConverter conv = new BrushConverter();
            SolidColorBrush brush = conv.ConvertFromString(average.ToString()) as SolidColorBrush;
            Background = brush;

            // JJP
            // I've tried everthing I can think of to set the background of the image to another color other than black but nothing seems to work.
            // this sets the surrounding padding around the border to the right color but I haven't been able to set area inside the border's background color
            // I think it must be something simple but I'm not seeing it.
            // Its like the background color is already set and whatever I do I cannot change it.
            
            icon.MouseLeftButtonUp +=new MouseButtonEventHandler(icon_MouseLeftButtonUp);
            icon.MouseRightButtonUp += new MouseButtonEventHandler(icon_MouseRightButtonUp);
            Border.Child = icon;
            
        }

        Color GetAverageColor(BitmapImage image)
        {
            long red = 0;
            long green = 0;
            long blue = 0;
            long alpha = 0;// Transerancy 0 = totaly transparent, 255 = opaque
            long count = 0;
            
            Color returnVal = new Color();
            int height = image.PixelHeight;
            int width = image.PixelWidth;
            int stride = (width * image.Format.BitsPerPixel + 7) / 8;
            Byte[] Pixels = new Byte[height * stride];
            try
            {
                image.CopyPixels(Pixels, stride, 0);

                for (int i = 0; i < stride * height; i += image.Format.BitsPerPixel)
                {
                    if (Pixels[i] != 0)//don't count the color if it is completely transparent
                    {
                        alpha += Pixels[i];
                        red += Pixels[i + 1];
                        green += Pixels[i + 2];
                        blue += Pixels[i + 3];
                        count++;
                    }
                }
            }
            catch (Exception e)
            {

            }
            if (count > 0)
            {
                returnVal.R = (byte)(red / count);
                returnVal.G = (byte)(green / count);
                returnVal.B = (byte)(blue / count);
                returnVal.A = 255;// opaque
            }
            else //White
            {
                returnVal.R = 255;
                returnVal.G = 255;
                returnVal.B = 255;
                returnVal.A = 255;
            }
            return returnVal;
        }

        void icon_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            _tile.Delete();
        }

        void icon_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _tile.LaunchApp();
        }
    }
}
